package de.tum.beilmaier.im.client.controller;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.IsSerializable;

import de.novanic.eventservice.client.event.Event;
import de.novanic.eventservice.client.event.RemoteEventService;
import de.novanic.eventservice.client.event.RemoteEventServiceFactory;
import de.novanic.eventservice.client.event.listener.RemoteEventListener;
import de.tum.beilmaier.im.client.InstantChat;
import de.tum.beilmaier.im.client.MessagingService;
import de.tum.beilmaier.im.client.MessagingServiceAsync;
import de.tum.beilmaier.im.client.UserSessionHandler;
import de.tum.beilmaier.im.client.event.CreateChatRoomEvent;
import de.tum.beilmaier.im.client.event.IncomingMessageEvent;
import de.tum.beilmaier.im.client.view.ChatRoomWindow;
import de.tum.beilmaier.im.client.view.ContactListWindow;
import de.tum.beilmaier.im.shared.model.InstantMessage;


public class ChatRoomController implements ClickHandler {
	
	private ChatRoomWindow chatRoomWindow = null;
	private UserSessionHandler userhandler = null;
	private InstantChat imChat = null;
	
	public ChatRoomController(ChatRoomWindow chatRoomWindow, UserSessionHandler userhandler, InstantChat imChat) {
	
		this.chatRoomWindow = chatRoomWindow;
		this.userhandler = userhandler;
		this.imChat = imChat;
		chatRoomWindow.addSendButtonListener(this);
		
	}

	@Override
	public void onClick(ClickEvent event) {
		InstantMessage im = new InstantMessage(userhandler.getLoginUser(), chatRoomWindow.getSendBox().getText(), chatRoomWindow.getChatRoom());
		chatRoomWindow.getSendBox().setText(""); 
		System.out.println("Send Message To Server.." + im.getChatRoom().getUUID().toString());
		
		// trigger new create Chatroom event
				MessagingServiceAsync messagingService = GWT.create(MessagingService.class);
				messagingService.sendInstantMessagetoChatRoom(im, new AsyncCallback<Void>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(Void result) {
						
					}
				});
}

}